package scientistengineer.blogspot.affrpor;



import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class PlayTextListAdapter extends ArrayAdapter<PlayTextRow> {
	
	int layoutResourceId;
	PlayTextRow data[] = null;
	Context context;

	public PlayTextListAdapter(Context context, int resource, PlayTextRow [] data) {
		super(context, resource, data);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = resource;
		this.data = data;
		
	}
	
	
	private OnClickListener onPortClickListener;
	private OnClickListener onAffrClickListener;
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		PlayTextRowHolder holder = null;
		
		if (row == null)
		{
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent,false);
			
			holder = new PlayTextRowHolder(row);
			
			holder.playPort = (ImageButton)row.findViewById(R.id.iconPlay_Port);
			holder.textPort = (TextView)row.findViewById(R.id.textView1);
			holder.textAffr = (TextView)row.findViewById(R.id.textView2);
			holder.playAffr = (ImageButton) row.findViewById(R.id.iconPlay_Affr);
			
			row.setTag(holder);
		}
		else
		{
			holder = (PlayTextRowHolder)row.getTag();
		}
		
		PlayTextRow singleRow = data[position];		
		holder.playPort.setImageResource(singleRow.playIconPort);
		holder.textPort.setText(singleRow.textPort);
		holder.textAffr.setText(singleRow.textAffr);
		holder.playAffr.setImageResource(singleRow.playIconAffr);
		
		holder.playPort.setOnClickListener(this.onPortClickListener);
		holder.playAffr.setOnClickListener(this.onAffrClickListener);
		
		return row;
	}
	
	public void setOnPortClickListener(final OnClickListener onClickListener) {
		
	    this.onPortClickListener = onClickListener;
	}
	
	public void setOnAffrClickListener(final OnClickListener onClickListener) {
		
	    this.onAffrClickListener = onClickListener;
	}
	
	public static class PlayTextRowHolder
	{
		private View mRow;
		ImageButton playPort;
		TextView textPort;
		TextView textAffr;
		ImageButton playAffr;
		
		public PlayTextRowHolder(View row)
		{
			mRow = row;
		}
		
		public ImageButton getSubImageButtonPort()
		{
			if(null == playPort)
			{
				playPort = (ImageButton) mRow.findViewById(R.id.iconPlay_Port);
			}
			return(playPort);
		}
		
		public ImageButton getSubImageButtonAffr()
		{
			if(null == playAffr)
			{
				playAffr = (ImageButton) mRow.findViewById(R.id.iconPlay_Affr);
			}
			return(playAffr);
		}
	}
}
